package com.sisqorap.wakeuplight;

import java.util.Calendar;

import com.sisqorap.TimeTools;
import com.sisqorap.Preferences;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Alarm {
	private Context packageContext;
	
	private final short AT_FIRST_ALARM = 0;
	
	private final String PF_FIRST_ALARM = "fa";
	
	private short SECONDS = 60;
	private int MILLISECONDS = 1000;
	
	private int snoozeTime = 0;
	
	private Preferences pref;
	
	public Alarm(Context packageContext){
		this.packageContext = packageContext;
		pref = new Preferences(packageContext);
		Settings setting = new Settings(packageContext);
		snoozeTime = setting.getSnoozeTime();
	}
	
	public Boolean isFirstAlarmSet(){
		return isAlarmSet(AT_FIRST_ALARM);
	}
	
	private Boolean isAlarmSet(short ALARMTYPE){
		String prefix = "";
		switch(ALARMTYPE){
		case AT_FIRST_ALARM:
			prefix = PF_FIRST_ALARM;
			break;
		}
		int alarmYear = pref.getIntValue(prefix + "Year", 0);
		int alarmMonth = pref.getIntValue(prefix + "Month", 0);
		int alarmDay = pref.getIntValue(prefix + "Day", 0);
		int alarmHour = pref.getIntValue(prefix + "Hour", 0);
		int alarmMinute = pref.getIntValue(prefix + "Minute", 0);
		Calendar checkCalendar = Calendar.getInstance();
		Calendar testCalendar = Calendar.getInstance();
		testCalendar.set(alarmYear, alarmMonth, alarmDay, alarmHour, alarmMinute, 0);
		Boolean isset = false;
		if(testCalendar.getTimeInMillis() > checkCalendar.getTimeInMillis()){
			isset = true;
		}
		return isset;
	}
	
	public String setFirstAlarm(int alarmHour, int alarmMinute){
		return setAlarm(alarmHour, alarmMinute, AT_FIRST_ALARM, true);
	}

	public String setFirstAlarmSnooze(int alarmHour, int alarmMinute){
		return setAlarm(alarmHour, alarmMinute, AT_FIRST_ALARM, false);
	}

	private String setAlarm(int alarmHour, int alarmMinute, short ALARMTYPE, Boolean saveSettings){
		Intent intent = new Intent();
		switch(ALARMTYPE){
			case AT_FIRST_ALARM:
				intent = new Intent(packageContext, ShowNotification.class);
				break;
		}
        PendingIntent sender = PendingIntent.getBroadcast(packageContext, 0, intent, 0);
		Calendar today = Calendar.getInstance();
        int alarmDay = today.get(Calendar.DAY_OF_MONTH);
        int alarmMonth = today.get(Calendar.MONTH);
        int alarmYear = today.get(Calendar.YEAR);
		Calendar alarmCalendar = Calendar.getInstance();
        alarmCalendar.setTimeInMillis(System.currentTimeMillis());
        //Date alarmTime = new Date(year, month, day, hour, minute);
        alarmCalendar.set(alarmYear, alarmMonth, alarmDay, alarmHour, alarmMinute, 0);
        Settings setting = new Settings(packageContext);
		snoozeTime = setting.getSnoozeTime();
        alarmCalendar.add(Calendar.MINUTE, -snoozeTime);
        if(alarmCalendar.before(today)){
        	alarmCalendar.add(Calendar.DAY_OF_MONTH, 1);
        	alarmYear = alarmCalendar.get(Calendar.YEAR);
        	alarmMonth = alarmCalendar.get(Calendar.MONTH);
        	alarmDay = alarmCalendar.get(Calendar.DAY_OF_MONTH);
        }
        // Schedule the alarm!
        AlarmManager am = (AlarmManager)packageContext.getSystemService(Context.ALARM_SERVICE);
        String prefix = "";
        int replyString = 0;
        switch(ALARMTYPE){
			case AT_FIRST_ALARM:
				am.setRepeating(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), SECONDS*MILLISECONDS, sender);
				prefix = PF_FIRST_ALARM;
				replyString = R.string.timespan;
				break;
		}
		// Store new primitive types in the shared preferences object.
        if(saveSettings){
			pref.setIntValue(prefix + "Hour", alarmCalendar.get(Calendar.HOUR_OF_DAY));
			pref.setIntValue(prefix + "Minute", alarmCalendar.get(Calendar.MINUTE));
			pref.setIntValue(prefix + "Day", alarmCalendar.get(Calendar.DAY_OF_MONTH));
			pref.setIntValue(prefix + "Month", alarmCalendar.get(Calendar.MONTH));
			pref.setIntValue(prefix + "Year", alarmCalendar.get(Calendar.YEAR));
        }
        today = Calendar.getInstance();
        long timeBeforeAlarm = alarmCalendar.getTimeInMillis() - today.getTimeInMillis();
        StringBuilder sb = new StringBuilder(packageContext.getString(replyString).replace("[TIMESPAN]", TimeTools.millisToTimeString(timeBeforeAlarm)));
        return sb.toString();
	}
	
	public String getFirstAlarmTime(){
		return getAlarmTime(AT_FIRST_ALARM);
	}
	
	private String getAlarmTime(short ALARMTYPE){
		String prefix = "";
		switch(ALARMTYPE){
		case AT_FIRST_ALARM:
			prefix = PF_FIRST_ALARM;
			break;
		}
		int alarmHour = pref.getIntValue(prefix + "Hour", 0);
		int alarmMinute = pref.getIntValue(prefix + "Minute", 0);
		StringBuilder sb = new StringBuilder(pad(alarmHour)).append(":").append(pad(alarmMinute));
		return sb.toString();
	}
	
	public void endFirstAlarm(){
		removeFirstAlarm();
	}
	
	public void removeFirstAlarm(){
		removeAlarm(AT_FIRST_ALARM);
	}
	
	private void removeAlarm(short ALARMTYPE){
		Intent intent = new Intent();
		String prefix = "";
		switch(ALARMTYPE){
			case AT_FIRST_ALARM:
				intent = new Intent(packageContext, ShowNotification.class);
				prefix = PF_FIRST_ALARM;
				break;
		}
        PendingIntent sender = PendingIntent.getBroadcast(packageContext, 0, intent, 0);
        AlarmManager am = (AlarmManager)packageContext.getSystemService(Context.ALARM_SERVICE);
        am.cancel(sender);
        pref.setIntValue(prefix + "Year", (Calendar.getInstance().get(Calendar.YEAR)-1));
	}
	
	public int getFirstAlarmHour(){
		return getAlarmHourOrMinute(PF_FIRST_ALARM + "Hour"); 
	}
	
	public int getFirstAlarmMinute(){
		return getAlarmHourOrMinute(PF_FIRST_ALARM + "Minute"); 
	}
	
	private int getAlarmHourOrMinute(String key){
		return pref.getIntValue(key, 0);
	}

	public String SnoozeFirstAlarm(){
		endFirstAlarm();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MINUTE, snoozeTime);
        return setFirstAlarmSnooze(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
	}
	
	private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
}